package com.sharefang.aproject.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sharefang.aproject.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2016/8/10.
 */

public abstract class BaseActivity extends AppCompatActivity {
    Toolbar toolbar;
    TextView tv_center;
    ImageButton ib_left, ib_right;
    TabLayout tabLayout;
    RelativeLayout rl_userInfo;
    Drawable drawable;

    CircleImageView civ_userHeader;
    TextView tv_author, tv_description, tv_guanzhu, tv_funs;
    ImageButton ib_edit;

    abstract void findView();

    abstract void initView();

    abstract void setLayout();

    /**
     * 记录处于前台的Activity
     */
    private static BaseActivity mForegroundActivity = null;
    /**
     * 记录所有活动的Activity
     */
    private static final List<BaseActivity> mActivities = new LinkedList<BaseActivity>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.d(this.getClass() + "----------------------onCreate");
        setLayout();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_center = (TextView) findViewById(R.id.tv_center);
        ib_left = (ImageButton) findViewById(R.id.ib_left);
        ib_right = (ImageButton) findViewById(R.id.ib_right);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        rl_userInfo = (RelativeLayout) findViewById(R.id.rl_userInfo);

        drawable = toolbar.getBackground();

        civ_userHeader = (CircleImageView) findViewById(R.id.civ_userHeader);
        tv_author = (TextView) findViewById(R.id.tv_author);
        tv_description = (TextView) findViewById(R.id.tv_description);
        tv_guanzhu = (TextView) findViewById(R.id.tv_guanzhu);
        tv_funs = (TextView) findViewById(R.id.tv_funs);
        ib_edit = (ImageButton) findViewById(R.id.ib_edit);

        findView();
        initView();
    }

    public void setCenterTitle(String title) {
        tv_center.setText(title);
    }

    public void setHeaderImage(String url) {
        Picasso.with(this)
                .load("http:" + url)
                .placeholder(R.mipmap.folder)
                .error(R.mipmap.default_head_img)
                .fit()
                .centerCrop()
                .into(civ_userHeader);
    }

    public void setTv_author(String author) {
        tv_author.setText(author);
    }

    public void setTv_description(String description) {
        tv_description.setText(description);
    }

    public void setGuanzhu(int gz) {
        tv_guanzhu.setText("关注 " + gz);
    }

    public void setFuns(int fs) {
        tv_funs.setText("粉丝 " + fs);
    }

    public void setLeftBtn(int resId) {
        ib_left.setVisibility(View.VISIBLE);
        ib_left.setImageResource(resId);
    }

    public void setRightBtn(int resId) {
        ib_right.setVisibility(View.VISIBLE);
        ib_right.setImageResource(resId);
    }

    public void visibleRlUserInfo(boolean visible) {
        if (visible) {
            rl_userInfo.setVisibility(View.VISIBLE);
            toolbar.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else {
            rl_userInfo.setVisibility(View.GONE);
            toolbar.setBackground(drawable);

        }

    }

    public void visibleTabLayout(boolean visible) {
        if (visible)
            tabLayout.setVisibility(View.VISIBLE);
        else
            tabLayout.setVisibility(View.GONE);
    }

    ViewPager mViewPager;

    public void setTabModeFixed(boolean fixed) {
        if (fixed)
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
        else
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    public void setViewPager(ViewPager viewPager) {
        if (this.mViewPager != null)
            this.mViewPager = null;
        this.mViewPager = viewPager;
        tabLayout.setupWithViewPager(this.mViewPager);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Logger.d(this.getClass() + "----------------------onStart");

    }

    @Override
    protected void onResume() {
        mForegroundActivity = this;
        super.onResume();
        Logger.d(this.getClass() + "----------------------onResume");

    }

    @Override
    protected void onPause() {
        Logger.d(this.getClass() + "----------------------onPause");
        mForegroundActivity = null;
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Logger.d(this.getClass() + "----------------------onStop");

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Logger.d(this.getClass() + "----------------------onRestart");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Logger.d(this.getClass() + "----------------------onDestory");

    }

    /**
     * 关闭所有Activity
     */
    public static void finishAll() {
        List<BaseActivity> copy;
        synchronized (mActivities) {
            copy = new ArrayList<BaseActivity>(mActivities);
        }
        for (BaseActivity activity : copy) {
            activity.finish();
        }
    }

    /**
     * 关闭所有Activity，除了参数传递的Activity
     */
    public static void finishAll(BaseActivity except) {
        List<BaseActivity> copy;
        synchronized (mActivities) {
            copy = new ArrayList<BaseActivity>(mActivities);
        }
        for (BaseActivity activity : copy) {
            if (activity != except)
                activity.finish();
        }
    }

    /**
     * 是否有启动的Activity
     */
    public static boolean hasActivity() {
        return mActivities.size() > 0;
    }

    /**
     * 获取当前处于前台的activity
     */
    public static BaseActivity getForegroundActivity() {
        return mForegroundActivity;
    }

    /**
     * 获取当前处于栈顶的activity，无论其是否处于前台
     */
    public static BaseActivity getCurrentActivity() {
        List<BaseActivity> copy;
        synchronized (mActivities) {
            copy = new ArrayList<BaseActivity>(mActivities);
        }
        if (copy.size() > 0) {
            return copy.get(copy.size() - 1);
        }
        return null;
    }

    /**
     * 退出应用
     */
    public static void exitApp() {
        finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
